define([], function() {
  'use strict';



  return function(url, params) {

    var xhr = new XMLHttpRequest();

    params || (params = { method: 'GET' });
    // 发送请求
    xhr.open(params.method.toLowerCase(), url, true);

    var header = params.headers || params.header;
    if (typeof header == 'object') {
      for (var key in header) {
        xhr.setRequestHeader(key.toLowerCase(), header[key]);
      }
    }

    // form-data开启上传
    if (params.body instanceof FormData) {
      // 上传开始
      xhr.upload.onloadstart = function() {};
      // 上传结束
      xhr.upload.onloadend = function() {};
    }

    // 发送数据
    params.body && xhr.send(params.body);


    xhr.json = function() {
      var that = this;

      return new Promise((resolve, reject) => {
        xhr.onloadend = function() {
          var json = that.responseText;
          typeof json == 'object' && resolve(json) || resolve(JSON.parse(json));
        };
      });
    };

    return new Promise((resolve, reject) => {
      xhr.onerror = function(e) {
        reject(e);
      };
      return resolve(xhr);
    });
  };
});
